import { FilterConst } from "../../../../component/gconst/FilterConst";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { Language } from "../../../../../extend/language/Language";
import { NpcEffectRule } from "../../../rules/NpcEffectRule";
import { ObjCreate } from "../../common/ObjCreate";
import { CopyConfig } from "../../../resource/CopyConfig";
import { CopyUtil } from "../../copy/CopyUtil";
import { Cache } from "../../../cache/Cache";
import { Global } from "../../../../../com/gengine/global/Global";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { SecTimer } from "../../../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../../../com/gengine/core/frame/TimerType";
import { CrossBeachData } from "../../crossBeach/CrossBeachData";
import { RemainTimeTool } from "../../award/RemainTimeTool";
import { GuideArrowManager } from "../../../manager/GuideArrowManager";
import { EffectManager } from "../../../manager/EffectManager";
import { OperateInfo } from "../../../model/OperateInfo";
import { ENpcEffect } from "../../../../../Message/Public/ENpcEffect";
import { BrowerUtil } from "../../../../../com/gengine/utils/BrowerUtil";
import { SoundManager } from "../../../../common/sound/SoundManager";
import { SoundNameConst } from "../../../../common/sound/SoundNameConst";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { GuideArrow } from "../../guide/GuideArrow";
import { LineBox } from "../../guide/LineBox";
import { GCheckBox } from "../../../../../com/mui/controls/GCheckBox";
import { GTextFiled } from "../../../../../com/mui/controls/GTextFiled";
type int = number;
//class OperateItem
    
    export  class OperateItem /* flash.display.Sprite */
    {
        constructor()
        {
            
            buttonMode = true;
            this._operateSp = new Object /* flash.display.Sprite */();
            this.addChild(this._operateSp);
            this._operateIcon = new Object /* flash.display.Bitmap */();
            this._operateSp.addChild(this._operateIcon);
            this._operateText = new Object /* flash.text.TextField */();
            this._operateText.textColor = 16776960;
            this._operateText.width = 270;
            this._operateText.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._operateText.selectable = false;
            this._operateText.x = 22;
            this._operateText.filters = [FilterConst.nameGlowFilter];
            this.addChild(this._operateText);
            this._operateSp.addEventListener(flash.events.MouseEvent.CLICK, this.onOptTxtLinkHandler);
            this._operateText.addEventListener(flash.events.TextEvent.LINK, this.onOptTxtLinkHandler);
            addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddToStageHandler);
            return;
        }

        protected createExtOptTxt(arg1: string): void
        {
            if (arg1 && !(arg1 == "")) 
            {
                if (!this._extOptTxt) 
                {
                    this._extOptTxt = new Object /* flash.text.TextField */();
                    this._extOptTxt.mouseEnabled = true;
                    this._extOptTxt.selectable = false;
                    this._extOptTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
                    this._extOptTxt.width = 250;
                    this._extOptTxt.filters = [FilterConst.nameGlowFilter];
                    this._extOptTxt.addEventListener(flash.events.TextEvent.LINK, this.onExtOptLinkHandler);
                }
                if (!contains(this._extOptTxt)) 
                {
                    addChild(this._extOptTxt);
                }
                this._extOptTxt.x = this._operateText.x + this._operateText.textWidth + 10;
                this._extOptTxt.htmlText = arg1;
            }
            return;
        }

        protected createExtOptBtn(): void
        {
            if (this._operateInfo && (this._operateInfo.delegateAble || this._operateInfo.defenceDelegate)) 
            {
                if (!this._extOptBtn) 
                {
                    this._extOptBtn = new GButton();
                    this._extOptBtn.setSize(46, 19);
                    this._extOptBtn.styleName = "DialogOptBtn2";
                    this._extOptBtn.textField.filters = [FilterConst.nameGlowFilter];
                    this._extOptBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onExtOptBtnClickHandler);
                }
                if (!contains(this._extOptBtn)) 
                {
                    this.addChild(this._extOptBtn);
                    this._extOptBtn.x = this._operateText.x + this._operateText.textWidth + 4;
                    this._extOptBtn.y = 1;
                }
                if (this._operateInfo.defenceDelegate) 
                {
                    this._extOptBtn.label = Language.getString(69712);
                }
                else 
                {
                    this._extOptBtn.label = Language.getString(20981);
                }
                if (this._extOptTxt) 
                {
                    this._extOptTxt.x = this._extOptBtn.x + this._extOptBtn.width + 5;
                }
            }
            else if (NpcEffectRule.isMusicPlay(this._operateInfo.operateId)) 
            {
                if (!this._extOptBtn) 
                {
                    this._extOptBtn = new GButton();
                    this._extOptBtn.setSize(46, 19);
                    this._extOptBtn.styleName = "DialogOptBtn2";
                    this._extOptBtn.textField.filters = [FilterConst.nameGlowFilter];
                    this._extOptBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onExtOptBtnClickHandler);
                }
                this._extOptBtn.label = Language.getString(26003);
                if (!contains(this._extOptBtn)) 
                {
                    this.addChild(this._extOptBtn);
                    this._extOptBtn.x = this._operateText.x + this._operateText.textWidth + 4;
                    this._extOptBtn.y = 1;
                }
                if (!this._extOptBtn1) 
                {
                    this._extOptBtn1 = new GButton();
                    this._extOptBtn1.setSize(46, 19);
                    this._extOptBtn1.styleName = "DialogOptBtn2";
                    this._extOptBtn1.textField.filters = [FilterConst.nameGlowFilter];
                    this._extOptBtn1.addEventListener(flash.events.MouseEvent.CLICK, this.onExtOptBtn1ClickHandler);
                }
                this._extOptBtn1.label = Language.getString(26004);
                if (!contains(this._extOptBtn1)) 
                {
                    this.addChild(this._extOptBtn1);
                    this._extOptBtn1.x = this._extOptBtn.x + this._extOptBtn.width + 4;
                    this._extOptBtn1.y = this._extOptBtn.y;
                }
            }
            return;
        }

        protected createExtraCb(arg1: int): void
        {
            if (!this._extraTimeCb) 
            {
                this._extraTimeCb = ObjCreate.createCheckBox(Language.getString(60354), 100, 0, 100, 20, this);
                this._extraTimeCb.mouseEnabled = true;
                this._extraTimeCb.addEventListener(flash.events.Event.CHANGE, this.onExtraTimeChange);
            }
            if (!contains(this._extraTimeCb)) 
            {
                addChild(this._extraTimeCb);
            }
            var loc1=this._extOptTxt && this._extOptTxt.parent ? this._extOptTxt.textWidth + 20 : 20;
            loc1 = this._extOptBtn && this._extOptBtn.parent ? loc1 + this._extOptBtn.width : loc1;
            this._extraTimeCb.x = this._operateText.x + this._operateText.textWidth + loc1;
            var loc2=CopyConfig.instance.getInfoByCode(arg1);
            var loc3=CopyUtil.getCopyNumByDay(loc2) - Cache.instance.copy.getHasEnterNumById(arg1);
            this._extraTimeCb.toolTipData = Language.getStringByParam(60469, loc2.extraDropCost);
            if (loc3 >= 2 || Global.isDebugModle) 
            {
                this._extraTimeCb.enabled = true;
                this._extraTimeCb.filters = [];
                this._extraTimeCb.selected = Cache.instance.copy.getCopyExtraTime(arg1) > 0 ? true : false;
            }
            else 
            {
                this._extraTimeCb.enabled = false;
                this._extraTimeCb.selected = false;
                this._extraTimeCb.filters = [FilterConst.colorFilter2];
                Dispatcher.dispatchEvent(new DataEvent(EventName.CopySetCopyExtraTime, {"copyId": arg1, "value": 0}));
            }
            return;
        }

        protected createJumpFloorCb(arg1: int): void
        {
            if (!this._jumpFloorCb) 
            {
                this._jumpFloorCb = ObjCreate.createCheckBox("", 200, 0, 100, 20, this);
                this._jumpFloorCb.mouseEnabled = true;
                this._jumpFloorCb.addEventListener(flash.events.Event.CHANGE, this.onJumpFloorChange);
            }
            if (!contains(this._jumpFloorCb)) 
            {
                addChild(this._jumpFloorCb);
            }
            var loc1=this._extOptTxt && this._extOptTxt.parent ? this._extOptTxt.textWidth + 20 : 20;
            var loc2=this._extraTimeCb && this._extraTimeCb.parent ? 75 : 0;
            this._jumpFloorCb.x = this._operateText.x + this._operateText.textWidth + loc1 + loc2;
            if (CopyUtil.toWhichFloor != 0) 
            {
                this._jumpFloorCb.selected = true;
                this._jumpFloorCb.label = Language.getStringByParam(60393, CopyUtil.toWhichFloor);
            }
            else 
            {
                this._jumpFloorCb.selected = false;
                this._jumpFloorCb.label = Language.getString(60392);
            }
            var loc3=CopyConfig.instance.getInfoByCode(arg1);
            var loc4;
            if ((loc4 = CopyUtil.getCopyNumByDay(loc3) - Cache.instance.copy.getHasEnterNumById(arg1)) > 0 || Global.isDebugModle) 
            {
                this._jumpFloorCb.enabled = true;
                this._jumpFloorCb.filters = [];
            }
            else 
            {
                this._jumpFloorCb.enabled = false;
                this._jumpFloorCb.filters = [FilterConst.colorFilter2];
            }
            return;
        }

        protected createTimerText(): void
        {
            if (this._timerText == null) 
            {
                this._timerText = ObjCreate.createGTextField("", 210, 0, 80, 20, this, GlobalStyle.textFormat6);
                this._timerText.x = this._operateText.x + this._operateText.textWidth + 5;
            }
            if (this._secTimer == null) 
            {
                this._secTimer = new SecTimer();
                this._secTimer.addListener(TimerType.ENTERFRAME, this.onTimerEnterFrameHandler);
                this._secTimer.addListener(TimerType.COMPLETE, this.onTimerCompleteHandler);
            }
            this._secTimer.repeatCount = CrossBeachData.getSunBathRestime(this._operateInfo.operateId);
            this._secTimer.start();
            this.onTimerEnterFrameHandler();
            return;
        }

        /* internal  */onTimerEnterFrameHandler(arg1: SecTimer=null): void
        {
            if (this._timerText) 
            {
                this._timerText.text = "(" + RemainTimeTool.getRemainTimeBySeconds(this._secTimer.repeatCount, false, false) + ")";
            }
            return;
        }

        /* internal  */onTimerCompleteHandler(arg1: SecTimer): void
        {
            arg1.stop();
            arg1.isDelete = false;
            return;
        }

        /* internal  */onSunBathPassSuccessHandler(arg1: DataEvent): void
        {
            this.resetSecTimer();
            return;
        }

        /* internal  */onSunBathMakeUpSuccessHandler(arg1: DataEvent): void
        {
            this.resetSecTimer();
            return;
        }

        /* internal  */resetSecTimer(): void
        {
            if (this._secTimer) 
            {
                this._secTimer.reset();
                this._secTimer.repeatCount = CrossBeachData.getSunBathRestime(this._operateInfo.operateId);
                this._secTimer.start();
                this.onTimerEnterFrameHandler();
            }
            return;
        }

        public dispose(): void
        {
            if (this._guideArrow) 
            {
                GuideArrowManager.hideGuideArrow();
            }
            EffectManager.disposeLineBox(this._lineBox);
            this._operateInfo = null;
            this._operateText.htmlText = "";
            if (this._extOptTxt && contains(this._extOptTxt)) 
            {
                removeChild(this._extOptTxt);
            }
            if (this._extraTimeCb && contains(this._extraTimeCb)) 
            {
                removeChild(this._extraTimeCb);
            }
            if (this._jumpFloorCb && contains(this._jumpFloorCb)) 
            {
                removeChild(this._jumpFloorCb);
            }
            if (this._extOptBtn && contains(this._extOptBtn)) 
            {
                removeChild(this._extOptBtn);
            }
            if (this._extOptBtn1 && contains(this._extOptBtn1)) 
            {
                removeChild(this._extOptBtn1);
            }
            if (this._secTimer) 
            {
                this._secTimer.stop();
                this._secTimer.dispose();
                this._secTimer = null;
            }
            if (this._timerText && contains(this._timerText)) 
            {
                removeChild(this._timerText);
                this._timerText = null;
            }
            return;
        }

        public get operateInfo(): OperateInfo
        {
            return this._operateInfo;
        }

        public /* override */ get height(): number
        {
            return 22;
        }

        protected onOptTxtLinkHandler(arg1: Object /* flash.events.Event */): void
        {
            if (this._operateInfo.operateId == ENpcEffect._ENpcEffectYourLover) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.SunBathPass, 0));
                return;
            }
            if (this._operateInfo.operateId == ENpcEffect._ENpcEffectCrossDefenceBook) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.CrossDefenceShowGuideBook));
                return;
            }
            this.dispatchEvent(new Object /* flash.events.Event */(EventName.NpcOperateTextClick, true));
            return;
        }

        protected onExtOptLinkHandler(arg1: Object /* flash.events.TextEvent */): void
        {
            if (arg1.text) 
            {
                if (arg1.text != "TaskDelegation") 
                {
                    if (arg1.text == "CopyPractice") 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.CopyEnterPracticeCopy, this._operateInfo.copyId));
                    }
                }
                else 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShowDelegateTaskWindow, this._operateInfo.copyId));
                }
            }
            return;
        }

        protected onExtOptBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._operateInfo) 
            {
                if (NpcEffectRule.isMusicPlay(this._operateInfo.operateId)) 
                {
                    this.dispatchEvent(new Object /* flash.events.Event */(EventName.NpcOperateTextClick, true));
                }
                else if (this._operateInfo.delegateAble || this._operateInfo.defenceDelegate) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShowDelegateTaskWindow, this._operateInfo.copyId));
                }
            }
            return;
        }

        protected onExtOptBtn1ClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._operateInfo) 
            {
                if (NpcEffectRule.isMusicPlay(this._operateInfo.operateId)) 
                {
                    BrowerUtil.getUrl(SoundManager.instance.getUrlByName(SoundNameConst.getDownloadNameByNpcEffect(this._operateInfo.operateId)));
                }
            }
            return;
        }

        protected onExtraTimeChange(arg1: Object /* flash.events.Event */=null): void
        {
            var loc1=this._extraTimeCb.selected ? 1 : 0;
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopySetCopyExtraTime, {"copyId": this._operateInfo.copyId, "value": loc1}));
            return;
        }

        protected onExtraTimeCbAlertHandler(arg1: int): void
        {
            if (arg1 == Alert.CANCEL) 
            {
                this._extraTimeCb.selected = false;
                Dispatcher.dispatchEvent(new DataEvent(EventName.CopySetCopyExtraTime, {"copyId": this._operateInfo.copyId, "value": 0}));
            }
            return;
        }

        /* internal  */onJumpFloorChange(arg1: Object /* flash.events.Event */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopySetFloorCbChange, {"selected": this._jumpFloorCb.selected, "code": this._operateInfo.copyId}));
            return;
        }

        /* internal  */onAddToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            if (this._operateInfo && !(this._operateInfo.copyId == 0)) 
            {
                NetDispatcher.addCmdListener(ServerCommand.Copy_CopyNumInfoChange, this.onCopyNumInfoChange);
            }
            if (this._operateInfo && this._operateInfo.operateId == ENpcEffect._ENpcEffectYourLover) 
            {
                Dispatcher.addEventListener(EventName.SunBathPassSuccess, this.onSunBathPassSuccessHandler);
            }
            if (this._operateInfo && this._operateInfo.operateId == ENpcEffect._ENpcEffectSunBathMakeUp) 
            {
                Dispatcher.addEventListener(EventName.SunBathMakeUpSuccess, this.onSunBathMakeUpSuccessHandler);
            }
            addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.onRemoveFromStageHandler);
            return;
        }

        /* internal  */onRemoveFromStageHandler(arg1: Object /* flash.events.Event */): void
        {
            removeEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.onRemoveFromStageHandler);
            NetDispatcher.removeCmdListener(ServerCommand.Copy_CopyNumInfoChange, this.onCopyNumInfoChange);
            Dispatcher.removeEventListener(EventName.SunBathPassSuccess, this.onSunBathPassSuccessHandler);
            Dispatcher.removeEventListener(EventName.SunBathMakeUpSuccess, this.onSunBathMakeUpSuccessHandler);
            return;
        }

        /* internal  */onCopyNumInfoChange(arg1: Object): void
        {
            if (this._operateInfo) 
            {
                this._operateInfo.updateData();
                this.updateData(this._operateInfo);
            }
            return;
        }

        public updateData(arg1: OperateInfo): void
        {
            this._operateInfo = arg1;
            this._operateIcon.bitmapData = GlobalClass.getBitmapData(arg1.icon);
            this._operateText.htmlText = HTMLUtil.addColor("<u><a href=\'event: 0\'>" + arg1.operateDes + "</a></u>" + " " + HTMLUtil.addColor(arg1.extendDes, "#00ff00"), "#ffff00");
            this.createExtOptTxt(this._operateInfo.extendOpt);
            this.createExtOptBtn();
            if (arg1.showExtraCb) 
            {
                this.createExtraCb(this._operateInfo.copyId);
            }
            if (arg1.showJumpFloor) 
            {
                this.createJumpFloorCb(this._operateInfo.copyId);
            }
            if (Cache.instance.guide.guidePetRefresh && this._operateInfo.operateId == ENpcEffect._ENpcEffectPlayPetGrouth || Cache.instance.guide.guideEquipRefresh && this._operateInfo.operateId == ENpcEffect._ENpcEffectPlayWeaponStrengthen) 
            {
                this.showGuideArrow(this._operateText.x + this._operateText.textWidth + 10, this.height / 2);
                this.showLinBoxRoundEffect();
            }
            else if (Cache.instance.guide.guidePetSite) 
            {
                if (this._operateInfo.operateId != ENpcEffect._ENpcEffectPlayPetSingleSire) 
                {
                    if (this._operateInfo.operateId == ENpcEffect._ENpcEffectPlayPetGroupSire) 
                    {
                        this.showLinBoxRoundEffect();
                    }
                }
                else 
                {
                    this.showGuideArrow(this._operateText.x + this._operateText.textWidth + 10, this.height + 1);
                    this.showLinBoxRoundEffect();
                }
            }
            if (arg1.operateId == ENpcEffect._ENpcEffectYourLover || arg1.operateId == ENpcEffect._ENpcEffectSunBathMakeUp) 
            {
                this.createTimerText();
            }
            return;
        }

        public showGuideArrow(arg1: int=-1, arg2: int=-1): void
        {
            this._guideArrow = GuideArrowManager.getGuideArrow(GuideArrowManager.Dir_L, null, 0);
            this._guideArrow.x = arg1;
            this._guideArrow.y = arg2;
            if (arg1 == -1) 
            {
                this._guideArrow.x = this._operateText.x + this._operateText.textWidth + 10;
            }
            if (arg2 == -1) 
            {
                this._guideArrow.y = this.height / 2;
            }
            addChild(this._guideArrow);
            return;
        }

        public showLinBoxRoundEffect(): void
        {
            this._lineBox = EffectManager.getLineBox(this._operateIcon.width + this._operateText.textWidth, this.height, [65280, 16711680]);
            this._lineBox.x = this._operateIcon.x;
            this._lineBox.y = this._operateIcon.y;
            addChild(this._lineBox);
            return;
        }

        protected /* var */_operateSp: Object /* flash.display.Sprite */;

        protected /* var */_operateText: Object /* flash.text.TextField */;

        protected /* var */_operateIcon: Object /* flash.display.Bitmap */;

        protected /* var */_operateInfo: OperateInfo;

        protected /* var */_guideArrow: GuideArrow;

        protected /* var */_lineBox: LineBox;

        protected /* var */_extOptTxt: Object /* flash.text.TextField */;

        protected /* var */_extOptBtn: GButton;

        protected /* var */_extOptBtn1: GButton;

        protected /* var */_extraTimeCb: GCheckBox;

        protected /* var */_jumpFloorCb: GCheckBox;

        protected /* var */_secTimer: SecTimer;

        protected /* var */_timerText: GTextFiled;
    }
